home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-08-16 | 5.4 KB | 130 lines | [TEXT/MPS ] |
- #----------------------------------------------------------------------------------------------------------------------------------------------------
- # Comment: comments selected text
- # MPW Shell Script
- # Written by Gina Cherry • August 16, 1991
- # Copyright: © 1991 by Apple Computer, Inc., all rights reserved.
- #
- # Usage:
- # Comment [window]
- #
- # Function:
- # Comment inserts the appropriate commenting characters for a selected block of text in the
- # specified window. If no window is specified, Comment operates on the target window.
- # Comment will comment the entire line the cursor is on.
- #
- # Note:
- # Comment can be added to the Edit menu with the following command:
- #
- # AddMenu Edit "Comment/3" 'Comment "{Active}"'
- #----------------------------------------------------------------------------------------------------------------------------------------------------
-
- # If more than 1 parameter is given, write an error message and exit script.
- If {#} > 1
- Echo "###Usage: {0} filename"
- Exit 1
- End >> Dev:StdErr
-
- # Do not exit on error.
- Set Exit 0
-
- # Initialize variable for file name extension.
- Set suffix ""
-
- # Set Window to the parameter, if one is given. Otherwise set Window to the target window.
- If {#} == 1
- Set Window "{1}"
- Else
- Set Window "{Target}"
- End
-
-
- # Get file extension.
-
- # If Window consists of a series of characters followed by a '.', the suffix is the letters following
- # the '.'.
- If "{Window}" =~/≈.([a-z]+)®1/
- Set suffix "{®1}"
- End
-
-
- # Choose the appropriate commenting characters, depending on the type of file being commented.
- # These variable assignments can be changed to customize the commenting format.
-
- # C or C++ file
- If ("{suffix}" =~ /[chri]/) || ("{suffix}" == "cp")
- Set startComment '/*****************************************************************************\ '
- Set endComment '\*****************************************************************************/'
- Set midComment ' * '
-
- # Pascal file
- Else if "{suffix}" == 'p'
- Set startComment '{-------------------------------------------------------------------------------'
- Set endComment '-------------------------------------------------------------------------------}'
- Set midComment ' # '
-
- # Assembly file
- Else if "{suffix}" == "a"
- Set startComment ';-------------------------------------------------------------------------------'
- Set endComment ';-------------------------------------------------------------------------------'
- Set midComment '; '
-
- # MPW script or other file
- Else
- Set startComment '#-------------------------------------------------------------------------------'
- Set endComment '#-------------------------------------------------------------------------------'
- Set midComment '# '
- End
-
- # Set nLines to the number of lines of selected text in the input file. All diagnostic output is
- # discarded. The Count command will fail if Window is not a valid window name. The Echo and Exit
- # commands will be executed if and only if the Count command fails. In that case, an error message
- # is written to standard output and the script is exited.
- Set nLines `(count -l "{Window}.§" ≥ Dev:Null) || ∂
- (Echo "### {0}: {Window} not a valid window name." >> Dev:StdErr; Exit 1)` ≥ Dev:Null
-
- # Position cursor at the beginning of the selected text in the input file.
- Find Δ§ "{Window}"
-
- # Decrement nLines, because the last line is a special case.
- Evaluate nLines -= 1
-
- # Position cursor at the beginning of the line the cursor is currently on.
- Find Δ`position -l "{Window}"` "{Window}"
-
- # Mark the beginning of the commented text for later use.
- Mark -y § {0}.Comments "{Window}"
-
- # Prepend the startComment string to the text to be commented.
- Replace § "{startComment}∂n" "{Window}"
-
- # Prepend the comment character(s) to all lines to be commented except the last line. Replace the
- # existing text on the current line with the midComment string followed by the original text.
- # The ≈ (option x) character represents all characters on the current line up to but not including
- # the newline character. Thus, the ®1 variable takes on the value of the original line of text. The
- # ∂n (newline) character is included in the regular expression so that the cursor is positioned at
- # the beginning of a new line each time replace is executed. If the cursor was not positioned at the
- # beginning of a new line, the midComment string would be appended to the same line nLines-1
- # times.
- Replace -c {nLines} /(≈∂n)®1/ "{midComment}®1" "{Window}"
-
- # The last line is a special case because the previous replace command will not work if there is no
- # newline character at the end of the last line of the text to be commented (i.e. if the last line of the
- # text to be commented is also the last line of the input file).
- Replace /(≈)®1/ "{midComment}®1" "{Window}"
-
- # Append the endComment string to the selected text.
- Replace § "∂n{endComment}" "{Window}"
-
-
- # Clean up.
-
- # Position cursor at the beginning of the commented block.
- Find Δ{0}.Comments "{Window}"
-
- # Select the entire block of commented text.
- Find /"{startComment}"/:/"{endComment}"/ "{Window}"
-
- # Delete the marker from Window.
- Unmark {0}.Comments "{Window}"
-
-